#include "main.hpp"
#include "dahua_dev_list.hpp"
#include "manager_adapter.hpp"

manager_adapter::manager_adapter( int fd ){
	state = FALSE;
	sock.m_nSock = fd;
}

manager_adapter::manager_adapter(){
	sock.m_nSock = STDIN_FILENO;
}

manager_adapter::~manager_adapter(){

	if( sock.m_nSock >= 0 ){
		close( sock.m_nSock );
	}
}

BOOL
manager_adapter::is_manager_quit(){
	return state;
}

void 
manager_adapter::quit(){
	state = TRUE;
	return;
}

BOOL
manager_adapter::get_params( ){

	char buffer[ STUB_MAX_MSG_LEN ];
	STUBPARAM *pParam = ( STUBPARAM * ) buffer;
	MSG_HEADER *pHeader = ( MSG_HEADER * ) buffer;

#ifdef _USEDAEMON
		if ( msg_recv( sock.m_nSock, buffer, STUB_MAX_MSG_LEN ) <= 0 ) {
			SAY( DP_ERROR, "<ERROR_LOG>read wrong param message %d\n" , sock.m_nSock );
			return -1;
		}
//		pParam->nDeviceVersion = DAHUA_STUB_DVR0804LB_S;
#else
		pHeader->nCmd = STUB_MSG_PARAM;
		pParam->nNodeID = 3;
		strcpy( pParam->sNodeName, "dahua-test" );
		pParam->nDeviceVersion = DAHUA_STUB_DVR0804LB_S;
//		strcpy( pParam->sConnectIP, "192.168.15.108" );
		strcpy( pParam->sConnectIP, "192.168.15.132" );
//		strcpy( pParam->sConnectIP, "192.168.15.191" );
		pParam->nConnectPort = 37777;
		strcpy( pParam->sConnectUserName, "system" );
		strcpy( pParam->sConnectPassword, "system" );
#endif

	if ( pHeader->nCmd != STUB_MSG_PARAM ) {
		SAY( DP_ERROR, "<ERROR_LOG>read wrong param message nCmd=%d\n", pHeader->nCmd );
		return -1;
	}

	memcpy( &params , pParam , sizeof(STUBPARAM) );

	return TRUE;
}

int
manager_adapter::action(int arg0, void * arg1){

	
	if( arg0 < 0 ){
		SAY( DP_DEBUG , "action arg0 == -1\n");
		quit();
		return -1;
	}

	sigset_t zeromask;
	sigemptyset( &zeromask );
	sigaddset( &zeromask, CHILD_QUIT_SIGNAL );
	sigaddset( &zeromask, CHILD_CON_SIGNAL );
	sigprocmask( SIG_UNBLOCK, &zeromask, NULL );

	sigemptyset( &zeromask );
	sigaddset( &zeromask, SIGTERM );
	sigaddset( &zeromask, SIGIO );
	sigprocmask( SIG_BLOCK, &zeromask, NULL );


	char buffer[ STUB_MAX_MSG_LEN ];
	MSG_HEADER *pHeader = ( MSG_HEADER * ) buffer;

	SAY( DP_ERROR, "manager_adapter::action enter\n" );

	if ( msg_recv( sock.m_nSock, buffer, STUB_MAX_MSG_LEN ) <= 0 ) {
//	if ( read( sock.m_nSock, buffer, STUB_MAX_MSG_LEN ) <= 0 ) {
		SAY( DP_ERROR, "<ERROR_LOG> manager_adapter::action read wrong message \n" );
		quit();
		return -1;
	}
	SAY( DP_TRACE, "receive from pipe nCmd=%d\n", pHeader->nCmd );
	if ( pHeader->nCmd == STUB_MSG_HEARTBEAT ) {
		SAY( DP_TRACE, "receive heartbeat\n" );
		if ( xsend( STDOUT_FILENO, ( char* ) pHeader, pHeader->nLength ) != ( int ) pHeader->nLength ) {
//		if ( write( sock.m_nSock, ( char* ) pHeader, pHeader->nLength ) != ( int ) pHeader->nLength ) {
			SAY( DP_TRACE, "send to pipe nCmd=%d\n", pHeader->nCmd );
			quit();
			return -1;
		}
	}

//	if ( !xstub_p->is_quit() ) xstub_p->quit();
	SAY( DP_TRACE, "manager_adapter::action quit\n" );

	return 0;
}

void pipe_thread( unsigned long arg0, unsigned long arg1 ){

	SAY( DP_TRACE, "pipe thread enter\n" );

	sigset_t zeromask;
	sigemptyset( &zeromask );
	sigaddset( &zeromask, CHILD_QUIT_SIGNAL );
	sigaddset( &zeromask, CHILD_CON_SIGNAL );
	sigprocmask( SIG_UNBLOCK, &zeromask, NULL );

	sigemptyset( &zeromask );
	sigaddset( &zeromask, SIGTERM );
	sigaddset( &zeromask, SIGIO );
	sigprocmask( SIG_BLOCK, &zeromask, NULL );

	char buffer[ STUB_MAX_MSG_LEN ];
	MSG_HEADER *pHeader = ( MSG_HEADER * ) buffer;

	manager_adapter * manager_p = (manager_adapter *)arg0;

#ifndef _USEDAEMON
	int flags = fcntl( manager_p->sock.m_nSock , F_GETFD );
	if( flags< 0 ){
		SAY( DP_DEBUG , "fnctl F_GETFD ERROR\n" );
		goto out;
	}

	if (fcntl( manager_p->sock.m_nSock , F_SETFL, flags & (~O_NONBLOCK)) < 0){ 
		SAY( DP_DEBUG , "fnctl F_SETFL ERROR\n" );
		goto out;
	}
 
#endif 
	while ( manager_p ) {
#ifndef _USEDAEMON
		if ( read( manager_p->sock.m_nSock, buffer, STUB_MAX_MSG_LEN ) <= 0 ) {
#else
		if ( msg_recv( manager_p->sock.m_nSock, buffer, STUB_MAX_MSG_LEN ) <= 0 ) {
#endif
			SAY( DP_ERROR, "<ERROR_LOG> pipe_thread read wrong message %d\n" , manager_p->sock.m_nSock );
			break;
		}
		SAY( DP_TRACE, "receive from pipe nCmd=%d\n", pHeader->nCmd );
		if ( pHeader->nCmd == STUB_MSG_HEARTBEAT ) {
			SAY( DP_TRACE, "receive heartbeat\n" );
			if ( xsend( STDOUT_FILENO, ( char* ) pHeader, pHeader->nLength ) != ( int ) pHeader->nLength ) {
				SAY( DP_TRACE, "send to pipe nCmd=%d\n", pHeader->nCmd );
				break;
			}
		}else{
			SAY( DP_DEBUG , "unknown cmd\n");
			break;
		}
	}
#ifndef _USEDAEMON
out:	
#endif
	manager_p->quit();
//	if ( !is_quit() ) send_quit_event();
	SAY( DP_TRACE, "pipe_thread quit\n" );
    return ;
}

